package com.cskaoyan.wordmemorize.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.converter.LevelConverter;
import com.cskaoyan.wordmemorize.dao.entity.LevelDO;
import com.cskaoyan.wordmemorize.dao.mapper.LevelMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.UserVocAISituationDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.LevelService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class LevelServiceImpl implements LevelService {
    @Resource
    LevelMapper levelMapper;
    @Resource
    LevelConverter levelConverter;

    // 添加等级(后台等级权限管理-等级管理)
    @Override
    public void addLevel(LevelCommand command) {
        LevelDO levelDO = levelConverter.levelCommandToLevelDO(command);
        List<LevelDO> levelDOS = levelMapper.selectList(null);
        Optional<Long> reduce = levelDOS.stream().map(level -> level.getId()).reduce((o1, o2) -> o1 > o2 ? o1 : o2);
        levelDO.setId(reduce.get() + 1);
        levelMapper.insert(levelDO);
    }

    // 修改等级(后台等级权限管理-等级管理)
    @Override
    public void updateLevel(LevelCommand command) {
        LevelDO levelDO = levelConverter.levelCommandToLevelDO(command);
        levelMapper.updateById(levelDO);
    }

    // 分页查询等级(后台等级权限管理-等级管理)
    @Override
    public PageDTO<LevelDTO> getLevelList(PageRequest request) {
        QueryWrapper<LevelDO> queryWrapper = new QueryWrapper<>();
        // List<LevelDO> levelDOS = levelMapper.selectList(null);
        Page<LevelDO> page = new Page<>(request.getPageNum(), request.getCount());
        if (request.getKeyword()!=null){
            queryWrapper.like("level_name", request.getKeyword());
        }
        levelMapper.selectPage(page,queryWrapper);
        return levelConverter.levelDOToPageDTO(page.getRecords(), (int) page.getTotal());
        // if (request.getKeyword() != null) {
        //     List list = levelMapper.selectList(queryWrapper);
        //     List<LevelDO> selectRecords = page.getRecords();
        //     return levelConverter.levelDOToPageDTO(selectRecords,list.size());
        // }
        // levelMapper.selectPage(page, null);
        // List<LevelDO> records = page.getRecords();
        // PageDTO<LevelDTO> levelDTOPageDTO = levelConverter.levelDOToPageDTO(records, levelDOS.size());
        // return levelDTOPageDTO;

    }

    // 删除等级(后台等级权限管理-等级管理)
    @Override
    public DeleteLevelOperateDTO deleteLevel(Long id) {
        LevelDO levelDO = levelMapper.selectById(id);
        levelMapper.deleteById(id);
        return levelConverter.levelDOToDeleteLevelOperateDTO(levelDO);
    }

    // 批量删除等级(后台等级权限管理-等级管理)
    @Override
    public List<DeleteLevelOperateDTO> deleteLevels(BatchDeleteCommand command) {
        List<String> ids = command.getIds();
        List<LevelDO> levelDOS = levelMapper.selectByIds(ids);
        levelMapper.deleteByIds(ids);
        return levelConverter.levelDOsToDeleteLevelOperateDTOs(levelDOS);
    }
}
